var express = require('express');
var router = express.Router();
var http = require('http');
var url = require('url');
router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
// 轮播的接口    localhost:3000/lunbo?time=1495519120152
router.get('/lunbo', function(req, res) {
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    let t = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/billboard/home?__t=' + t;
    http.get(url, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    });
})
router.get('/lilist', function(req, res) {
    console.log(1111);
    /* http://m.maizuo.com/v4/api/film/now-playing?__t=1495615064326&page=1&count=5  */
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    let im = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/film/now-playing?__t='+im+'&page=1&count=5';
     http.get(url, function(response) {
        var pathdata = '';
        response.on('data', function(chunk) {
            pathdata += chunk;
        })
        response.on('end', function() {
            res.send(pathdata);
        })
    });
})
//http://m.maizuo.com/v4/api/film/now-playing?page=1&count=7

router.get('/listL', function(req, res) {
    console.log(1111);
    /* http://m.maizuo.com/v4/api/film/now-playing?__t=1495615064326&page=1&count=5  */
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    let im = req.query.time;
    let loadtype=req.query.loadtype;
    var url = 'http://m.maizuo.com/v4/api/film/'+loadtype+'?page='+i+'&count=7';
     http.get(url, function(response) {
        let pathdata = '';
        response.on('data', function(chunk) {
            pathdata += chunk;
        })
        response.on('end', function() {
            res.send(pathdata);
        })
    });
})
/* http://m.maizuo.com/v4/api/film/coming-soon?__t=1495629068374&page=1&count=3 */
router.get('/bottom', function(req, res) {
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    let time = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/film/coming-soon?__t='+ time +'&page=1&count=3';
     http.get(url, function(respon) {
        var data = '';
        respon.on('data', function(chunk) {
            data += chunk;
        })
        respon.on('end', function() {
            res.send(data);
        })
    });
})

//'http://localhost:3000/'+this.$route.params.id+'?time'+new Date().getTime()
/* http://m.maizuo.com/v4/api/film/3664?__t=1495851985873 */
router.get(/\d$/, function(req, res) {
    var time = req.query.time;
    // console.log("aaa")
    //url node.js模块 需要引入url.pathname
    var id=url.parse(req.url).pathname.substr(1);
    console.log(id)
    var url1 = 'http://m.maizuo.com/v4/api/film/'+id+'?__t='+time;
     http.get(url1, function(respon) {
        var con = '';
        respon.on('data', function(chunk) {
            con += chunk;
        })
        respon.on('end', function() {
            res.send(con);
        })
    });
})





/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
module.exports = router;
